package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.mapper.DestinationMapper;
import cn.wolfcode.luowowo.article.mapper.RegionMapper;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.common.query.QueryObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@Service
public class DestinationServiceImpl implements IDestinationService{

  @Autowired
  private DestinationMapper destinationMapper;
  @Autowired
  private RegionMapper regionMapper;

    public PageInfo query(QueryObject qo) {
         PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Destination> list= destinationMapper.selectForList(qo);
        return new PageInfo(list);
    }

    public List<Destination> getToasts(Long parentId) {
        List<Destination> list=new ArrayList<>();
        createToast(parentId,list);
        return list;
    }



    private void createToast(Long parentId, List<Destination> list) {
        Destination dest = destinationMapper.selectByPrimaryKey(parentId);
        if(dest==null){
            return;
        }
        list.add(0,dest);

        if(dest.getParent()!=null){
           createToast(dest.getParent().getId(),list);
        }


    }

    public void changeHotState(Long id, Integer hot) {
        destinationMapper.changeHotState(id,hot);
    }

    public List<Destination> listDests() {
        List<Destination> list = destinationMapper.selectAll();
        return list;
    }

    public List<Destination> listDestByRegionId(Long rid) {
        Long[] refIds = regionMapper.selectByPrimaryKey(rid).getRefIds();
        return destinationMapper.selectByRefIds(refIds);
    }

    public List<Destination> listHotDestByRegionId(Long regionId) {

        return destinationMapper.selectHotDestByRegionId(regionId);
    }

    public List<Destination> list() {
        return destinationMapper.selectAll();
    }

    public Destination getCountry(Long destId) {

        List<Destination> toasts = getToasts(destId);

        return toasts.get(0);
    }

    public Destination getProvince(Long destId) {
        List<Destination> toasts = getToasts(destId);
        //有可能没有城市
        return toasts.size()>1?toasts.get(1):null;
    }


}
